#include "myini.h"


MyIni::MyIni(QObject *parent) : QObject(parent)
{

}

void MyIni::ensureConfigFile()
{
    //检查并创建 config.ini 文件
    QDir appDir = (QCoreApplication::applicationDirPath());
    QFile configFile(QDir(appDir).filePath("config.ini"));

    if (!configFile.exists()) {
        if (configFile.open(QIODevice::WriteOnly)) {
            configFile.close();
            qDebug() << "config.ini created successfully.";
        } else {
            qDebug() << "Failed to create config.ini!";
        }
    } else {
        qDebug() << "config.ini already exists.";
    }
}

QVector<ENCODE_PARAM> MyIni::loadVideoConfigs() {
    QVector<ENCODE_PARAM> configs;
    QDir appDir = (QCoreApplication::applicationDirPath());
    QSettings settings(QDir(appDir).filePath("config.ini"), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");

    for (int i = 0; i < 4; ++i) {
        QString group = QString("SaveConfig%1").arg(i);
        settings.beginGroup(group);

        ENCODE_PARAM config;
        config._camera = static_cast<CAMERA>(settings.value("camera", 0).toInt());

        config._srcIp = settings.value("srcIp", "127.0.0.1").toString();
        config._srcPort = settings.value("_srcPort", 1234).toUInt();

        config._dstIp = settings.value("dstIp", "127.0.0.1").toString();
        config._dstPort = settings.value("dstPort", 1234).toUInt();

        config._bindIp = settings.value("bindIp", "127.0.0.1").toString();
        config._bindPort = settings.value("bindPort", 1234).toUInt();

        config._fps = settings.value("fps", 30).toInt();

        configs.append(config);
        settings.endGroup();
    }
    qDebug() << "Configs load to config.ini";
    return configs;
}

ENCODE_PARAM MyIni::loadVideoConfig(CAMERA camera)
{
    QDir appDir = (QCoreApplication::applicationDirPath());
    QSettings settings(QDir(appDir).filePath("config.ini"), QSettings::IniFormat);
    settings.setIniCodec("UTF-8");

    settings.beginGroup(QString("SaveConfig%1").arg(static_cast<int>(camera)));

    ENCODE_PARAM config;
    config._camera = static_cast<CAMERA>(settings.value("camera", 0).toInt());

    config._srcIp = settings.value("srcIp", "127.0.0.1").toString();
    config._srcPort = settings.value("srcPort", 1234).toUInt();

    config._dstIp = settings.value("dstIp", "127.0.0.1").toString();
    config._dstPort = settings.value("dstPort", 1234).toUInt();

    config._bindIp = settings.value("bindIp", "127.0.0.1").toString();
    config._bindPort = settings.value("bindPort", 1234).toUInt();

    config._fps = settings.value("fps", 30).toInt();

    settings.endGroup();

    return config;
}
